package com.yb.xczx.cloud.auth.bean;

import com.alibaba.fastjson2.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.yb.xczx.cloud.auth.entity.SysPermission;
import com.yb.xczx.cloud.auth.entity.SysRole;
import com.yb.xczx.cloud.auth.entity.SysUser;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/17 17:18
 * FileName: XczxSysUser
 * Description:
 */

public class XczxSysUser extends User implements UserDetails {
    private Collection<GrantedAuthority> authorities;

    @JSONField(serialize = false)
    private List<SysRole> roles;


    @TableField(typeHandler = FastjsonTypeHandler.class)
    @JSONField(serialize = false)
    private List<SysPermission> permissions;


    private SysUser sysUser;

    private String loginAccessToken;

    private String oauth2AccessToken;

    private String loginType;


    public XczxSysUser(SysUser sysUser){
        super(sysUser.getUsername(),sysUser.getPassword(), AuthorityUtils.NO_AUTHORITIES);
        this.sysUser=sysUser;
    }

    public XczxSysUser(SysUser sysUser,Collection<? extends GrantedAuthority> authorities){
        super(sysUser.getUsername(),sysUser.getPassword(),sysUser.isEnabled(),sysUser.isAccountNonExpired(),sysUser.isCredentialsNonExpired(),sysUser.isAccountNonLocked(),authorities);
        this.authorities= (Collection<GrantedAuthority>) authorities;
        this.sysUser=sysUser;
    }
    public XczxSysUser(String username, String password, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, authorities);
        this.authorities= (Collection<GrantedAuthority>) authorities;
    }

    public XczxSysUser(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
        this.authorities= (Collection<GrantedAuthority>) authorities;
    }


    public void setAuthorities(Collection<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public List<SysRole> getRoles() {
        return roles;
    }

    public void setRoles(List<SysRole> roles) {
        this.roles = roles;
    }

    public List<SysPermission> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<SysPermission> permissions) {
        this.permissions = permissions;
    }

    public SysUser getSysUser() {
        return sysUser;
    }

    public void setSysUser(SysUser sysUser) {

        this.sysUser = sysUser;
    }

    public void setLoginAccessToken(String loginAccessToken) {
        this.loginAccessToken = loginAccessToken;
    }

    public void setOauth2AccessToken(String oauth2AccessToken) {
        this.oauth2AccessToken = oauth2AccessToken;
    }


    public String getLoginAccessToken() {
        return loginAccessToken;
    }

    public String getOauth2AccessToken() {
        return oauth2AccessToken;
    }

    public String getLoginType() {
        return loginType;
    }

    public void setLoginType(String loginType) {
        this.loginType = loginType;
    }
}
